<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="taskType">
        <el-select v-model="queryParams.taskType" clearable filterable placeholder="请选择类型">
          <el-option
                v-for="dict in taskTypeLists"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
                size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布状态" prop="publishStatus">
        <el-select v-model="queryParams.publishStatus" clearable filterable placeholder="请选择类型">
          <el-option
            v-for="dict in dict.type.publishStatus_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getParentingTask"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center"/> -->
      <el-table-column label="类型" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_type" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="简介" align="center" prop="info"/>

      <el-table-column label="示例图" align="center" prop="img">
        <template slot-scope="scope">
          <el-image
            style="width: 120px;height: 60px;"
            :src="scope.row.img[0]"
            :preview-src-list="[scope.row.img[0]]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="createBy"/>
      <el-table-column label="发布范围" align="center" prop="publishType">
        <template slot-scope="scope">
          <!-- <dict-tag :options="dict.type.publish_type" :value="scope.row.publishType"/> -->
          <span v-if="scope.row.publishType==0">教师</span>
        </template>
      </el-table-column>
       <el-table-column label="发布状态" align="center" prop="publishStatus">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.publishStatus_type" :value="scope.row.publishStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="完成情况" align="center" prop="finish">
        <template slot-scope="scope">
             <el-button
              @click.native.prevent="goToDetail(scope.row)"
              type="text"
              size="small">
              {{scope.row.finishNum}}/{{scope.row.totalNum}}
            </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.publishStatus==0"
            size="mini"
            type="text"
            @click="handlePublish(scope.row)"
          >发布</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getParentingTask"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form v-if="isAdd==1||isAdd==2" ref="form" :model="form" :rules="rules" label-width="120px">
        <div>
          <el-form-item label="任务类型:" prop="taskType">
            <el-select :disabled='isAdd==2' v-model="form.taskType" placeholder="任务类型">
              <el-option
                v-for="dict in taskTypeLists"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
                size="small">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标题:" prop="title">
                  <el-input v-model.trim="form.title" placeholder="请输入标题"/>
          </el-form-item>
          <el-form-item label="简介:" prop="info">
                  <el-input type="textarea" v-model.trim="form.info" placeholder="请输入简介"/>
          </el-form-item>
          <el-form-item label="上传示例图:" prop="img">
              <imageUpload v-model="form.img" :limit="20"/>
          </el-form-item>
          <el-form-item label="任务有效期:" prop="dateRange">
            <el-date-picker
              v-model="form.dateRange"
              size="small"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item v-if="isAdd==1" label="发布范围" prop="personType">
            <el-radio-group v-model="form.personType">
            <el-radio :label="0">全部</el-radio>
            <el-radio :label="1">指定</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="form.personType==1&&isAdd==1" label="人员类型:" prop="teacherIdList">
              <el-select v-model="form.teacherIdList" multiple placeholder="请选择">
                <el-option
                v-for="i in teacherList"
                :key="i.id"
                :label="i.name"
                :value="i.id">
                </el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div class="edit-form-main" v-else>
      <el-tabs  type="card"   tab-position="left" v-model="activename" @tab-click="handleClick" >
          <el-tab-pane  v-for="(editForm,indexs) in editList" :key="indexs" :lazy='true' :label="editForm.name" :name="indexs+''" >
            <span slot="label" :class="[editForm.statustatuss == 0 ? 'notFinish' : 'finish']">{{editForm.name}}</span>
            <el-form  ref="editForms" :model="editForm" :rules="rules" label-width="120px">
                <div class="task-type">
                  {{taskTypeLists.find(i=>i.dictValue==editForm.taskType).dictLabel}}
                  </div>
                <div class="task-main">
                    <el-form-item label="标题:" prop="title">{{editForm.title}}
                      <!-- <el-input v-model.trim="form.title" placeholder="请输入姓名"/> -->
                    </el-form-item>
                    <el-form-item label="简介:" prop="info">{{editForm.info}}
                      <!-- <el-input v-model.trim="form.title" placeholder="请输入姓名"/> -->
                    </el-form-item>
                    <el-form-item label="示例图:" prop="img">
                      <el-image :src="editForm.img"></el-image>
                      <!-- <el-input v-model.trim="form.title" placeholder="请输入姓名"/> -->
                    </el-form-item>
                    <el-form-item label="完成情况:" prop="info">{{editForm.info}}
                      <!-- <el-input type="textarea" v-model="editForm.info" placeholder="请输入评价"/> -->
                    </el-form-item>
                    <el-form-item label="上传完成图片:" prop="finishImg">
                      <el-image :src="editForm.finishImg"></el-image>
                      <!-- <imageUpload v-model="editForm.finishImg" :limit="1"/> -->
                    </el-form-item>
                    <el-form-item label="评价:" prop="teacherComment">
                      <el-input type="textarea" v-model="editForm.teacherComment" placeholder="请输入评价"/>
                    </el-form-item>
                    <div class="task-btn">
                      <el-button type="primary" @click="submitFormTask(editForm)">确 定</el-button>
                    </div>
                </div>
            </el-form>
          </el-tab-pane>
      </el-tabs>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="isAdd==1||isAdd==2" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudent,
  delStudent,
  addStudent,
  updateStudent,
  exportStudent,
  importTemplate
} from "@/api/base/student";
import { listTeacher } from "@/api/base/teacher";
import { getParentingTask,addParentingTask,getParentingTaskDetail,editParentingTask,editFinish } from "@/api/homeSystem/qztask";
import {selectClazz} from "@/api/base/clazz";
import {getToken} from "@/utils/auth";
import {mapGetters} from 'vuex'
import {cellphoneValidator} from "@/utils/nmbu";
import '@/utils/namereplace.js';
export default {
  name: "taskmbgl",
  dicts: [ 'year_type', 'task_type','publish_type','publishStatus_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //起始年
      // yearList:[],

      // 任务列表表格数据
      taskList: [],
      // 弹出层标题
      title: "新增任务",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // taskType: 1,
        taskTypeList:[2,3,4]
      },
      // 表单参数
      form: {
        taskType:null,
        title:'',
        info:'',
        img:'',
        publishType:0
      },
      editForm: {
        taskType:null,
        title:'',
        info:'',
        img:'',
        publishType:0
      },
      editList:[],
      // 表单校验
      rules: {
        taskType: [
          {required: true, message: "任务类型不能为空", trigger: "blur"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        info: [
          {required: true, message: "简介不能为空", trigger: "blur"}
        ],
        img: [
          {required: true, message: "示例图不能为空", trigger: "blur"}
        ],
        publishType: [
          {required: true, message: "发布范围不能为空", trigger: "blur"}
        ],
        personType:[{required: true, message: "发布范围不能为空", trigger: "blur"}],
        finishImg:[
          {required: true, message: "上传完成图片不能为空", trigger: "blur"}
        ],
        teacherComment:[
          {required: true, message: "评论不能为空", trigger: "blur"}
        ],
        dateRange:[
          {required: true, message: "发布时间不能为空", trigger: "blur"}
        ]
      },
      classList: [],
      activename: '0',//默认第一个
      isClassDisabled:true, //班级disabled
      publishTypeList:[
        {
          label:'班级',
          value:1
        },
        {
          label:'学校',
          value:2
        },
        {
          label:'区',
          value:3
        }
      ],
      selectTab:'0',
      teacherList:[],
      taskTypeLists:[],
      isAdd:1,//1新增2详情3查看完成情况
    };
  },
  computed: {
    ...mapGetters(['schoolCode']),
  },
  created() {
    this.getParentingTask();
    this.getListTeacher()
    this.getDicts("task_type").then(response => {
      this.taskTypeLists = response.data.filter(i=>i.dictValue!=1)
    });
  },
  methods: {
    // 修改
    handleUpdate(row){
      this.isAdd = 2
      this.reset();
      this.open = true;
      this.title = '修改任务'
      // Object.assign(this.form, row);
      this.form = {...row}
      // console.log('aaaa',row);
      this.$set( this.form,'dateRange',[row.startTime,row.endTime]);
      this.form.taskType = row.taskType+''
    },
    // 发布
    handlePublish(row){
      const params = {
        publishStatus:1,
        id:row.id
      }
      editParentingTask(params).then(res =>{
        if(res.code == 200){
          this.getParentingTask()
        }
      })
    },
    getListTeacher() {
      this.loading = true;
      listTeacher().then(response => {
        this.teacherList = response.rows;
        this.loading = false;
      });
    },
    handleClick(tab, event) {
      this.selectTab = tab.name
    },
    /**班级*/
    async getqueryFormclassList() {
      this.isClassDisabled = true
      this.form.classId = null
      try {
        let params = {}
        this.form.classId = ''
        if (this.form.year) params.year = this.form.year;
        const {rows} = await selectClazz(params);
        if(rows.length && this.form.year){
          this.isClassDisabled =  false
        }
        this.classList = rows.map(item => {
          return {
            id: item.id,
            label: item.title,
          }
        })
      } catch (e) {
        console.log(e)
      }
    },
    /**班级*/
    async getFormclassList() {
      try {
        let params = {}
        if (this.form.year) params.year = this.form.year;
        this.$set(this.form, 'classId', null)
        const {rows} = await selectClazz(params);
        this.classList = rows.map(item => {
          return {
            id: item.id,
            label: item.title,
          }
        })
      } catch (e) {
        console.log(e)
      }
    },
    /** 查询任务列表 */
    async getParentingTask() {
      try {
        this.loading = true;
        this.queryParams.schoolCode = this.$store.getters.schoolCode
        const data = await getParentingTask(this.queryParams)
        this.taskList = data.rows;
       this.taskList.forEach(item=>{
        item.img = item.img.split(',')
       })
       console.log( this.taskList,'------ this.taskList');
        this.total = data.total;
        this.loading = false;
      } catch (e) {
        console.log(e)
      }

    },
    // 跳转任务详情
    async goToDetail(row){
      this.isAdd = 3
      this.open = true;
      this.title = '任务详情'
      const params = {
        parentingTaskId:row.id
      }
      try {
        this.loading = true;
        const res = await getParentingTaskDetail(params)
        this.editList = res.data
        this.loading = false;
      } catch (e) {
        console.log(e)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        taskType:null,
        publishStatus:null,
        taskType:null,
        taskTypeList:[2,3,4]
      };
      // this.getFormclassList();
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // this.queryParams.taskTypeList = [this.queryParams.taskType]
      this.getParentingTask();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNum = 1;
      this.reset()
      this.getParentingTask()
      this.isClassDisabled = true
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isAdd = 1
      this.reset();
      this.open = true;
      this.title = '新增任务'
    },
    /** 新增提交按钮 */
    submitForm() {
      if(this.title == '新增任务'){
        this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.publishType = 0
            this.form.startTime = this.form.dateRange[0]
            this.form.endTime = this.form.dateRange[1]
            this.form.publishStatus = 0
            addParentingTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.form = {}
              this.getParentingTask();
            });
        }
      });
      }else {
        this.$refs["form"].validate(valid => {
        if (valid) {
            this.form.personType = 0
            this.form.startTime = this.form.dateRange[0]
            this.form.endTime = this.form.dateRange[1]
            this.form.publishStatus = 0
            editParentingTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getParentingTask();
            });
        }
      });
      }

    },
    submitFormTask(editForm){
      if(editForm.teacherComment){
        let params = {
          id:editForm.id,
          parentingTaskId:editForm.parentingTaskId,
          stuId:editForm.stuId,
          teacherComment:editForm.teacherComment
        }
        editFinish(params).then(response => {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getParentingTask();
         });
      }

    }
  }
};
</script>
<style lang="scss" scoped>
.el-dialog__body .el-select, .el-dialog__body .el-date-editor {
  width: 100%;
}
::v-deep .el-tabs__content{
    border: 1px solid #dfe4ed;
}
::v-deep .el-tabs--left .el-tabs__header.is-left{
  margin-right:0px;
}
::v-deep .el-tabs--left.el-tabs--card .el-tabs__item.is-left.is-active{
  border-right-color:#dfe4ed
}
::v-deep .el-tabs--left.el-tabs--card .el-tabs__nav{
  border-bottom: none;

}
::v-deep .el-tabs--left.el-tabs--card .el-tabs__item.is-left.is-active{
  border-right: none ;
}
::v-deep .el-tabs .el-tabs--card .el-tabs--left{
  border-right: 1px solid #dfe4ed!important;
}
::v-deep .el-tabs__content{
  border: none !important;;
}
::v-deep .el-tabs .notFinish {
  color: gary !important;;
}
::v-deep .el-tabs .finish {
  color: #007d53!important;;
}
.task-type{
  border: 1px solid #dfe4ed;
  height: 42px;
  text-align: center;
  line-height: 42px;
}
.task-main{
  padding-right: 10px;
  border: 1px solid #dfe4ed;
  border-top: none;
  .task-btn{
    text-align: right;
    margin: 10px;
  }
}
::v-deep .el-tabs__item{
  width: 100px;
}
.edit-form-main{
  position: relative;
}
::v-deep .el-tabs__item.is-active { // 选中的边框颜色设置， 权重最高
    border-left:1px solid #3792ff !important
}
</style>
